//
//  RegisterCityStarViewController.m
//  cityStar
//
//  Created by Hancong on 14/12/25.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "RegisterCityStarViewController.h"
#import <Foundation/Foundation.h>
#import "NSString+MD5.h"
#import <CommonCrypto/CommonDigest.h>

@interface RegisterCityStarViewController ()

@end

@implementation RegisterCityStarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"注册";
    [self addBackButton];
    self.btnSubMit.layer.cornerRadius = 4.0;
    self.btnSubMit.clipsToBounds = YES;
}

-(IBAction)getCaptchaCode:(id)sender
{
    NSString *methodName = [NSString stringWithFormat:@"%@%@",KInterFace_mobile,self.fieldPhoneNum.text];
    [HttpRequestManager getMethod:methodName parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发送验证码成功"];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}
- (NSString *)getMd5_32Bit_String:(NSString *)srcString{
    const char *cStr = [srcString UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    // CC_MD5( cStr, strlen(cStr), digest ); 这里的用法明显是错误的，但是不知道为什么依然可以在网络上得以流传。当srcString中包含空字符（\0）时
    CC_MD5( cStr, srcString.length, digest );
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02x", digest[i]];
    
    return result;
}

-(IBAction)submitRegister_Touch:(id)sender
{
//    if (self.fieldRecommend.text) {
//        <#statements#>
//    }
    
    NSString *addStrMD5 = [NSString stringWithFormat:@"%@%@%@",self.fieldPassWord.text,@"%",self.fieldPhoneNum.text];
    
//    NSString *stringed = [NSString stringWithMD5OfFile:addStrMD5];

    const char *cstr = [addStrMD5 UTF8String];
    unsigned char result[32];
//       CC_MD5( cstr, self.length, digest );
    CC_MD5( cstr, strlen(cstr), result );
    
    NSString *stringed = [NSString stringWithFormat:
                          @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                          result[0],result[1],result[2],result[3],
                          
                          result[4],result[5],result[6],result[7],
                          
                          result[8],result[9],result[10],result[11],
                          
                          result[12],result[13],result[14],result[15]
                          ];
    NSDictionary *parameters = [[NSDictionary alloc]init];
    if (self.fieldRecommend.text.length > 0) {
    parameters = @{@"user":self.fieldPhoneNum.text,
                                     @"password":[stringed lowercaseString],
                                     @"captch":self.fieldCaptcha.text,
                                     @"guider":self.fieldRecommend.text
                                     };
    }else{
    parameters = @{@"user":self.fieldPhoneNum.text,
                                 @"password":[stringed lowercaseString],
                                 @"captch":self.fieldCaptcha.text
                                 };
    }
    [HttpRequestManager postMethod:KInterFace_register parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"注册成功，请登录"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.fieldPassWord resignFirstResponder];
    [self.fieldPhoneNum resignFirstResponder];
    [self.fieldCaptcha resignFirstResponder];
    [self.fieldRecommend resignFirstResponder];
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
